package com.zyd.shiro.business.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.zyd.shiro.persistence.beans.User;
import com.zyd.shiro.persistence.dao.MallUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


import com.zyd.shiro.persistence.dao.MallUserAddressDao;
import com.zyd.shiro.persistence.beans.UserAddress;

/**
 * 服务层
 *
 * @author RookieWZW
 */
@Service
public class MallUserAddressService {

    @Autowired
    private MallUserAddressDao userAddressDao;

    @Autowired
    private MallUserDao userDao;

    public List<UserAddress> findAll() {

        return userAddressDao.findAll();
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    public Page<UserAddress> findPage(int page, int size) {
        PageRequest pageRequest = new PageRequest(page - 1, size);
        return userAddressDao.findAll(pageRequest);
    }

    private Specification<UserAddress> where(Map searchMap) {

        return new Specification<UserAddress>() {

            @Override
            public Predicate toPredicate(Root<UserAddress> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                    predicateList.add(cb.like(root.get("name").as(String.class), "%" + (String) searchMap.get("name") + "%"));
                }
                // 
                if (searchMap.get("phone") != null && !"".equals(searchMap.get("phone"))) {
                    predicateList.add(cb.like(root.get("phone").as(String.class), "%" + (String) searchMap.get("phone") + "%"));
                }
                // 细节
                if (searchMap.get("detail") != null && !"".equals(searchMap.get("detail"))) {
                    predicateList.add(cb.like(root.get("detail").as(String.class), "%" + (String) searchMap.get("detail") + "%"));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }

    public Page<UserAddress> findSearch(Map whereMap, int page, int size) {
        Specification<UserAddress> specification = where(whereMap);
        PageRequest pageRequest = new PageRequest(page - 1, size);
        return userAddressDao.findAll(specification, pageRequest);
    }

    public UserAddress findOne(Integer id) {
        return userAddressDao.findOne(id);
    }

    public void add(UserAddress userAddress) {

        userAddressDao.save(userAddress);
    }

    public void update(UserAddress userAddress) {
        userAddressDao.save(userAddress);
    }


    public void deleteList(Integer[] ids) {
        for (Integer id : ids) {
            userAddressDao.delete(id);
        }
    }

    public List<UserAddress> findByUserId(Integer user_id) {
        return userAddressDao.findByUser_id(user_id);
    }

    //TODO 可以传入实体对象
    public void save(Map infoMap) {
        UserAddress userAddress = new UserAddress();
        System.out.println( Integer.valueOf((String) infoMap.get("user_id"))+"~~~~~~~~~~~`````````~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        userAddress.setUser_id(Integer.valueOf((String) infoMap.get("user_id")));
        userAddress.setCity_id(Integer.valueOf((String) infoMap.get("city_id")));
        userAddress.setProvince_id(Integer.valueOf((String) infoMap.get("province_id")));
        userAddress.setRegion_id(Integer.valueOf((String)  infoMap.get("region_id")));
        userAddress.setWxapp_id(Integer.valueOf((String) infoMap.get("wxapp_id")));
        userAddress.setDetail((String) infoMap.get("detail"));
        userAddress.setName((String) infoMap.get("name"));
        userAddress.setPhone((String) infoMap.get("phone"));
        userAddressDao.save(userAddress);
    }

    public UserAddress findByUserIdAndAddressId(Integer user_id, Integer address_id) {

        return userAddressDao.findByUser_idAndId(user_id, address_id);
    }

    public void edit(Map infoMap, Integer  id) {

        UserAddress userAddress =userAddressDao.findOne(id);
        userAddress.setCity_id(Integer.valueOf((String) infoMap.get("city_id")));
        userAddress.setProvince_id(Integer.valueOf((String) infoMap.get("province_id")));
        userAddress.setRegion_id(Integer.valueOf((String)  infoMap.get("region_id")));

        userAddressDao.save(userAddress);
    }

    public void setDefault(Integer user_id, Integer address_id) {

        User user = userDao.findOne(user_id);
        user.setAddress_id(address_id);

        userDao.save(user);
    }

    public void delete(Integer user_id, Integer address_id) {
        User user = userDao.findOne(user_id);
        if (user.getAddress_id() == address_id) {
            user.setAddress_id(0);
        }
        userAddressDao.delete(address_id);
        userDao.save(user);
    }

}
